home *** CD-ROM | disk | FTP | other *** search
- /**\
- |**| µApp.c
- \**/
-
- /**\
- |**| compiler directives
- \**/
-
- #define SystemSevenOrLater 1
-
- /**\
- |**| Includes
- \**/
-
- #include <Devices.h>
- #include <Fonts.h>
- #include <CodeFragments.h>
- #include <Dialogs.h>
- #include <DiskInit.h>
- #include <LowMem.h>
- #include <fp.h>
- #include <PLStringFuncs.h>
- #include <Sound.h>
- #include <Traps.h>
- #include <Threads.h>
-
- #include <stdio.h>
- #include <string.h>
-
- #include "HappyTrails.h"
-
- /**\
- |**| typedefs, structs, enums, etc.
- \**/
-
- enum
- {
- mAppleMenu = 128,
- iAboutBox = 1,
- mFileMenu = 129,
- iQuit = 1,
- mTasksMenu = 130,
- mWeightMenu = 131
- };
-
- /**\
- |**| private (local) function prototypes
- \**/
-
- static OSErr Init_Mac(void);
- static void Handle_Command(long ms);
- static void Handle_Event(const EventRecord *pEventPtr);
- static void Handle_NullEvent(const EventRecord *pEventPtr);
- static void Handle_MouseEvent(const EventRecord *pEventPtr);
- static void Handle_KeyEvent(char key,SInt16 modifiers);
- static void Handle_UpdateEvent(WindowPtr updateWindowP);
- //static void Handle_ActivateEvent(WindowPtr updateWindowP);
- static void Handle_DiskEvent(long message);
- static void Handle_OSEvent(long message);
-
- static Boolean SetUp_MenuBar(void);
- static void Adjust_MenuItems(void);
- static void Adjust_Cursor(const Boolean pContextual);
-
- /**\
- |**| globals & constants
- \**/
-
- WindowPtr gWindowPtr = nil;
- Rect gWindowRect;
- Boolean gInBackGround = false;
- Rect gMenuRect = {0,0,0,0};
-
- static Boolean gQuitFlag = false;
-
- /**\
- |**| main
- \**/
-
- void main(void)
- {
- if (!Init_Mac() && SetUp_MenuBar() && !HT_Init(9))
- {
- do
- {
- EventRecord event;
-
- WaitNextEvent(everyEvent,&event,-1,nil);
- Adjust_Cursor((event.modifiers & controlKey) == controlKey);
- Handle_Event(&event);
- }
- while (!gQuitFlag);
-
- HT_Term();
- }
- }
-
- /**\
- |**| Initialize toolboxes
- \**/
-
- static OSErr Init_Mac(void)
- {
- OSErr error = noErr;
- #if TARGET_API_MAC_CARBON
- BitMap screenBits;
- #else
- SysEnvRec theWorld;
-
- //
- // Test the computer to be sure we can do color.
- // If not we would crash, which would be bad.
- // If we can’t run, just beep and exit.
- //
- error = SysEnvirons(1, &theWorld);
- if (theWorld.hasColorQD == false)
- {
- SysBeep(50);
- ExitToShell(); // If no color QD, we must leave.
- }
-
- MaxApplZone();
- InitGraf(&(qd.thePort));
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- #endif TARGET_API_MAC_CARBON
-
- InitContextualMenus();
-
- //
- // Make a new window for drawing in, and it must be a color window.
- // The window is full screen size, made smaller to make it more visible.
- //
-
- #if TARGET_API_MAC_CARBON
- gWindowRect = GetQDGlobalsScreenBits(&screenBits)->bounds;
- #else
- gWindowRect = qd.screenBits.bounds;
- #endif TARGET_API_MAC_CARBON
- // InsetRect(&gWindowRect, 20, 20);
- gWindowRect.top += GetMBarHeight() + 1;
-
- gWindowPtr = NewCWindow(nil, &gWindowRect, "\pHappy Trails!", true, documentProc,
- (WindowPtr) -1, false, 0);
-
- OffsetRect(&gWindowRect, -gWindowRect.left, -gWindowRect.top);
-
- #if TARGET_API_MAC_CARBON
- SetPort(GetWindowPort(gWindowPtr)); // set window to current graf port
- #else
- SetPort(gWindowPtr); // set window to current graf port
- #endif TARGET_API_MAC_CARBON
- TextSize(kTextSize); // smaller font for drawing.
-
- return error;
- }
-
- /**\
- |**| Menu hooks
- \**/
-
- static pascal short MyMBarHook(Rect *menuRect)
- {
- // RgnHandle tempRgn = NewRgn();
-
- gMenuRect = *menuRect;
-
- // RectRgn(tempRgn,menuRect);
- // CalcVisBehind(LMGetWindowList(),tempRgn);
- // InvertRgn(tempRgn);
-
- //YieldToAnyThread();
- // DisposeRgn(tempRgn);
- return 0;
- }
-
- static pascal void MyMenuHook(void)
- {
- // RgnHandle tempRgn = NewRgn();
- // RectRgn(tempRgn,&gMenuRect);
- // CalcVisBehind(LMGetWindowList(),tempRgn);
- // InvertRgn(tempRgn);
-
- //YieldToAnyThread();
- // DisposeRgn(tempRgn);
- }
-
- /**\
- |**| Setup menu bar
- \**/
-
- static Boolean SetUp_MenuBar(void)
- {
- Handle mBar = GetNewMBar(128); // handle to menu bar resource
-
- if (!ResError() && mBar)
- {
- SetMenuBar(mBar);
- AppendResMenu(GetMenuHandle(mAppleMenu),'DRVR');
- DrawMenuBar();
- ReleaseResource(mBar);
- Adjust_MenuItems();
-
- // LMSetMBarHook(NewMBarHookProc(MyMBarHook));
- // LMSetMenuHook(NewMenuHookProc(MyMenuHook));
-
- return true;
- }
- return false;
- }
-
- /**\
- |**| Adjust menu items
- \**/
-
- static void Adjust_MenuItems(void)
- {
- MenuHandle tMenuHdl = GetMenuHandle(mTasksMenu);
- if (tMenuHdl)
- {
- UInt16 index,count = CountMenuItems(tMenuHdl);
- for (index = 1;index <= count;index++)
- CheckMenuItem(tMenuHdl, index, index == gNumTasks);
- }
- }
-
- /**\
- |**| Adjust cursor
- \**/
-
- static void Adjust_Cursor(const Boolean pContextual)
- {
- if (pContextual)
- {
- static CursHandle tCursHandle = nil;
- if (!tCursHandle)
- tCursHandle = GetCursor(128); // kThemeContextualMenuArrowCursor?
- if (tCursHandle)
- SetCursor(*tCursHandle);
- }
- else
- #if TARGET_API_MAC_CARBON
- {
- Cursor arrowCursor;
- SetCursor(GetQDGlobalsArrow(&arrowCursor));
- }
- #else
- SetCursor(&qd.arrow);
- #endif TARGET_API_MAC_CARBON
- }
-
- /**\
- |**| Do menu command
- \**/
-
- static void Handle_Command(long ms)
- {
- short menuID = ms >> 16,
- menuItem = ms & 0xFFFF;
-
- switch (menuID)
- {
- case mAppleMenu:
- switch (menuItem)
- {
- case iAboutBox: // Bring up alert for About.
- SysBeep(15);
- break;
- default: // All non-About items in this menu are DAs.
- #if !TARGET_API_MAC_CARBON
- {
- Str255 daName;
- GetMenuItemText(GetMenuHandle(menuID),menuItem,daName);
- OpenDeskAcc(daName);
- }
- #endif TARGET_API_MAC_CARBON
- break;
- }
- break;
- case mFileMenu:
- switch (menuItem)
- {
- case iQuit:
- gQuitFlag = true;
- break;
- default:
- SysBeep(15);
- break;
- }
- break;
- case mTasksMenu:
- HT_Term();
- HT_Init(menuItem);
- break;
- case mWeightMenu: // Weight Menu
- break;
- }
- }
-
- /**\
- |**| Do event
- \**/
-
- static void Handle_Event(const EventRecord *pEventPtr)
- {
- switch (pEventPtr->what)
- {
- case nullEvent:
- Handle_NullEvent(pEventPtr);
- break;
- case mouseDown:
- Handle_MouseEvent(pEventPtr);
- break;
- case keyDown:
- case autoKey:
- dokey:
- Handle_KeyEvent((char)(pEventPtr->message & charCodeMask),pEventPtr->modifiers);
- break;
- case updateEvt:
- Handle_UpdateEvent((WindowPtr)pEventPtr->message);
- break;
- case diskEvt:
- Handle_DiskEvent(pEventPtr->message);
- break;
- case osEvt:
- Handle_OSEvent(pEventPtr->message);
- break;
- case kHighLevelEvent:
- AEProcessAppleEvent(pEventPtr);
- break;
- default:
- break;
- }
- }
-
- /**\
- |**| Do null event
- \**/
-
- static void Handle_NullEvent(const EventRecord *pEventPtr)
- {
- (pEventPtr); // #pragma unused (pEventPtr)
- if (gWindowPtr)
- {
- Rect tRect = {40,20,52,128};
- const RGBColor blackRGBColor = {0,0,0}, yellowRGBColor = {255,255,0};
- GrafPtr savePort;
-
- GetPort(&savePort);
- #if TARGET_API_MAC_CARBON
- SetPort(GetWindowPort(gWindowPtr));
- #else
- SetPort(gWindowPtr);
- #endif TARGET_API_MAC_CARBON
-
- HT_DoNull();
-
- SetPort(savePort);
- }
- }
-
- /**\
- |**| Do mousedown event
- \**/
-
- static void Handle_MouseEvent(const EventRecord *pEventPtr)
- {
- Boolean tInBackGround = gInBackGround;
- WindowPtr window;
- short part;
-
- gInBackGround = true; // suspend drawing while menus are displayed
- HT_DoNull();
-
- part = FindWindow(pEventPtr->where,&window);
-
- Adjust_Cursor(part == inContent);
-
- switch(part)
- {
- case inContent:
- {
- if (window != FrontWindow())
- SelectWindow(window);
- else
- HT_DoClick(pEventPtr);
- }
- break;
-
- case inMenuBar: // Process mouse menu command (if any).
- {
- long ms;
-
- Adjust_MenuItems();
- ms = MenuSelect(pEventPtr->where);
- if (ms)
- Handle_Command(ms);
- HiliteMenu(0); // Unhighlight what MenuSelect hilited.
- }
- break;
- #if !TARGET_API_MAC_CARBON
- case inSysWindow: // Let the system handle the mouseDown.
- SystemClick(pEventPtr,window);
- break;
- #endif TARGET_API_MAC_CARBON
- default:
- break;
- }
- gInBackGround = tInBackGround;
-
- SetRect(&gMenuRect, 0, 0, 0, 0);
- }
-
- /**\
- |**| Handle key events
- \**/
-
- static void Handle_KeyEvent(char key,SInt16 modifiers)
- {
- if ((modifiers & cmdKey) != 0)
- {
- Adjust_MenuItems();
- Handle_Command(MenuKey(key));
- HiliteMenu(0); // Unhighlight what MenuSelect hilited.
- }
- }
-
- /**\
- |**| Handle update events
- \**/
-
- static void Handle_UpdateEvent(WindowPtr updateWindowP)
- {
- GrafPtr savePort;
- RgnHandle visRgn;
-
- GetPort(&savePort);
- visRgn=NewRgn();
-
- #if TARGET_API_MAC_CARBON
- SetPort(GetWindowPort(updateWindowP));
-
- GetPortVisibleRegion(GetWindowPort(FrontWindow()), visRgn);
- #else
- SetPort(updateWindowP);
- visRgn = NewRgn();
- CopyRgn(updateWindowP->visRgn,visRgn);
- #endif TARGET_API_MAC_CARBON
-
- BeginUpdate(updateWindowP);
- UpdateControls(updateWindowP,visRgn);
- DrawGrowIcon(updateWindowP);
- EndUpdate(updateWindowP);
-
- SetPort(savePort);
-
- DisposeRgn(visRgn);
- }
-
- /**\
- |**| Handle disk events
- \**/
-
- static void Handle_DiskEvent(long message)
- {
- #if TARGET_API_MAC_CARBON
- (message);
- #else
- Point dialogLocation = {100,100};
-
- if ((message & 0xFFFF0000) != noErr)
- {
- DIBadMount(dialogLocation,message);
- }
- #endif TARGET_API_MAC_CARBON
- }
-
- /**\
- |**| Handle OS events
- \**/
-
- static void Handle_OSEvent(long message)
- {
- if ((message >> 24) == suspendResumeMessage)
- {
- if ((message & resumeFlag) != 0)
- gInBackGround = false;
- else
- gInBackGround = true;
- }
- }
-
-